import os
import shutil

def extract_files(source_dir, destination_dir):
    for root, dirs, files in os.walk(source_dir):
        for file_name in files:
            if file_name.endswith((".jpg", ".jpeg", ".png")):
                file_path = os.path.join(root, file_name)
                # print(file_path)
                # 构造目标路径
                dest_path = os.path.join(destination_dir, os.path.basename(file_path))
                # print(dest_path)

                # 复制文件到目标路径
                shutil.copy(file_path, dest_path)

                # 复制对应的JSON文件
                json_file_path = os.path.splitext(file_path)[0] + ".txt"
                if os.path.exists(json_file_path):
                    dest_json_path = os.path.splitext(dest_path)[0] + ".txt"
                    shutil.copy(json_file_path, dest_json_path)

# 源路径A和目标路径B
source_path =       "/home/champrin/Desktop/record_data/网盘/兵种内录/矿石数据集/2024-/aaa checked/checked/a"
destination_path =  "/home/champrin/Desktop/record_data/网盘/兵种内录/矿石数据集/2024-/aaa checked/checked/a - -"
os.makedirs(destination_path, exist_ok=True)

# 提取文件
extract_files(source_path, destination_path)